﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;

namespace Lists
{
    public class PhieuCanLamSang : DoiTuong
    {
        #region Guid LoaiPhieuID property
        public Guid LoaiPhieuID
        {
            get
            {
                if (_DataRow["LoaiPhieuID"] is Guid) return (Guid)_DataRow["LoaiPhieuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["LoaiPhieuID"] = value;
            }
        }
        #endregion

        #region string Ma property
        public string Ma
        {
            get
            {
                if (_DataRow["Ma"] is string) return (string)_DataRow["Ma"];
                return "";
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region DateTime Ngay property
        public DateTime Ngay
        {
            get
            {
                return (DateTime)DataRow["Ngay"];
            }
            set
            {
                _DataRow["Ngay"] = value;
            }
        }
        #endregion

        #region Guid DichVuID property
        public Guid DichVuID
        {
            get
            {
                if (_DataRow["DichVuID"] is Guid) return (Guid)_DataRow["DichVuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["DichVuID"] = value;
            }
        }
        #endregion

        #region Guid BacSiThucHienID property
        public Guid BacSiThucHienID
        {
            get
            {
                if (_DataRow["BacSiThucHienID"] is Guid) return (Guid)_DataRow["BacSiThucHienID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["BacSiThucHienID"] = value;
            }
        }
        #endregion

        #region Guid PhieuDangKiID property
        public Guid PhieuDangKiID
        {
            get
            {
                if (_DataRow["PhieuDangKiID"] is Guid) return (Guid)_DataRow["PhieuDangKiID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["PhieuDangKiID"] = value;
            }
        }
        #endregion

        #region Guid PhongBanID property
        public Guid PhongBanID
        {
            get
            {
                if (_DataRow["PhongBanID"] is Guid) return (Guid)_DataRow["PhongBanID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["PhongBanID"] = value;
            }
        }
        #endregion

        #region Guid NguoiLamPhieuID property
        public Guid NguoiLamPhieuID
        {
            get
            {
                if (_DataRow["NguoiLamPhieuID"] is Guid) return (Guid)_DataRow["NguoiLamPhieuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["NguoiLamPhieuID"] = value;
            }
        }
        #endregion

        #region bool DaXuLy property
        public bool DaXuLy
        {
            get
            {
                if (_DataRow["DaXuLy"] is bool) return (bool)_DataRow["DaXuLy"];
                return false;
            }
            set
            {
                _DataRow["DaXuLy"] = value;
            }
        }
        #endregion

        #region bool DaXuatVatTu property
        public bool DaXuatVatTu
        {
            get
            {
                if (_DataRow["DaXuatVatTu"] is bool) return (bool)_DataRow["DaXuatVatTu"];
                return false;
            }
            set
            {
                _DataRow["DaXuatVatTu"] = value;
            }
        }
        #endregion

        #region int State property
        public int State
        {
            get
            {
                if (_DataRow["State"] is int) return (int)_DataRow["State"];
                return -1;
            }
            set
            {
                _DataRow["State"] = value;
            }
        }
        #endregion

        #region string KetLuan property
        public string KetLuan
        {
            get
            {
                if (_DataRow["KetLuan"] is string) return (string)_DataRow["KetLuan"];
                return "";
            }
            set
            {
                _DataRow["KetLuan"] = value;
            }
        }
        #endregion

        public PhieuCanLamSang(DataRow dataRow)
            : base(dataRow)
        {
        }

        public DanhMucKhoanMucCanLamSang _DanhSachKhoanMucCanLamSang= new DanhMucKhoanMucCanLamSang();

        public void LoadKhoanMuc()
        {
            try
            {
                if (this == null) this.ID = Guid.Empty;
                _DanhSachKhoanMucCanLamSang.LoadFromDB(this.ID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }

    public class DanhMucPhieuCanLamSang : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucPhieuCanLamSang _Instance;
        public static DanhMucPhieuCanLamSang Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucPhieuCanLamSang();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    try
                    {
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView.Sort = "Ma DESC";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Clinic_PhieuCanLamSang";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new PhieuCanLamSang((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public PhieuCanLamSang this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new PhieuCanLamSang(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public PhieuCanLamSang NewDoiTuong()
        {
            try
            {
                return new PhieuCanLamSang(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public PhieuCanLamSang GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new PhieuCanLamSang(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public PhieuCanLamSang GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public PhieuCanLamSang GetByMa(string ma)
        {
            try
            {
                return GetByProperty("Ma", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucPhieuCanLamSang GetChuaXuatVatTu(bool sinhTuDong)
        {
            try
            {
                //Nếu sinh phiếu xuất vật tư mà LoadFromDB --> Các phiếu trên RAM bị lỗi hết
                if (!sinhTuDong)
                {
                    ChuanHoa_PhieuCanLamSangClinic_DaXuatVatTuByChungTu();
                    LoadFromDB();
                }
                string condition = "DaXuatVatTu = 0 OR DaXuatVatTu IS NULL";
                return Filter(condition);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucPhieuCanLamSang GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucPhieuCanLamSang GetByLoaiPhieuID(Guid loaiPhieuCanLamSangID)
        {
            try
            {
                string filter = string.Format("LoaiPhieuID = '{0}'", loaiPhieuCanLamSangID);
                return Filter(filter);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucPhieuCanLamSang Filter(string filterExpression)
        {
            try
            {
                DanhMucPhieuCanLamSang obj = new DanhMucPhieuCanLamSang();
                obj.DataView = new DataView(DanhMucPhieuCanLamSang.Instance.DataTable, filterExpression, "Ma DESC", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public void ChuanHoa_PhieuCanLamSangClinic_DaXuatVatTuByChungTu()
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = string.Format("UPDATE Clinic_PhieuCanLamSang SET DaXuatVatTu = 1 WHERE ID IN ( \n" +
                                                "SELECT B.ID FROM ChungTu A INNER JOIN Clinic_PhieuCanLamSang B ON A.Clinic_PhieuCanLamSangID = B.ID ) \n" +
                                                "AND DaXuatVatTu = 0 \n");
                cmd.ExecuteNonQuery();

                cmd.CommandText = string.Format("UPDATE Clinic_PhieuCanLamSang SET DaXuatVatTu = 0 WHERE ID NOT IN ( \n" +
                                                "SELECT B.ID FROM ChungTu A INNER JOIN Clinic_PhieuCanLamSang B ON A.Clinic_PhieuCanLamSangID = B.ID ) \n" +
                                                "AND DaXuatVatTu = 1 \n");
                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }
    }
}
